package com.ordering.dao.login.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.ordering.dao.login.UserLoginDao;
import com.ordering.entity.User;
import com.ordering.util.HibernateUtil;

public class UserLoginDaoImpl implements UserLoginDao
{
    private SessionFactory sessionFactory;
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    @Override
    public User login(User user)
    {
        Session s=null;
        try{
            s=HibernateUtil.getSession();
           
           //from后面是对象，不是表名
            String hql="from User as User where User.msisdn=? and User.password = ?";//使用命名参数，推荐使用，易读。
            Query query=s.createQuery(hql);
            query.setString(0, user.getMsisdn());
            query.setString(1, user.getPassword());
            
            List<User> userList =query.list();
            if(userList==null||userList.size()==0){
                return null;
            }
            for(User po:userList){
                return po;
            }
        }finally{
            if(s!=null)
                s.close();
        }
        return null;
    }

    @Override
    public User logout(User user)
    {
        return null;
    }
    
}
